package com.teacher.service.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.teacher.pojo.entity.Behavior;
import com.teacher.pojo.entity.FourPrinciple;
import com.teacher.pojo.excel.BehaviorExcel;
import com.teacher.pojo.excel.FourPrincipleExcel;
import com.teacher.service.IBehaviorService;
import com.teacher.service.IFourPrincipleService;
import com.teacher.service.IPerStudentService;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zfm
 * @Date: 2021/7/30 16:17
 */
public class ExcelFourPrincipleListener extends AnalysisEventListener<FourPrincipleExcel> {
    private Long termId;
    private IFourPrincipleService iFourPrincipleService;

    private List<FourPrinciple> fourPrincipleList = new ArrayList<>();
    private IPerStudentService iPerStudentService;

    public ExcelFourPrincipleListener(Long termId, IFourPrincipleService iFourPrincipleService, IPerStudentService iPerStudentService) {
        this.termId = termId;
        this.iFourPrincipleService = iFourPrincipleService;
        this.iPerStudentService = iPerStudentService;
    }

    @Override
    public void invoke(FourPrincipleExcel fourPrincipleExcel, AnalysisContext analysisContext) {
        if (iPerStudentService.isExit(fourPrincipleExcel.getStudentKey())) {
            FourPrinciple fourPrinciple = new FourPrinciple();
            BeanUtils.copyProperties(fourPrincipleExcel, fourPrinciple);
            fourPrinciple.setTermId(termId);
            fourPrincipleList.add(fourPrinciple);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
    }

    private void saveData() {
        iFourPrincipleService.saveBatch(fourPrincipleList);
    }
}
